namespace TraditionalMalteseCuisine.App_Code.Functions
{
    public class Hash_Functions
    {
        //*************************************************
        //              Class Members
        //*************************************************
        #region Class Members
        private const int MIN_VALUE = 32;
        private const int MAX_VALUE = 127;
        #endregion
        //*************************************************



        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        public static string HashPassword(string password)
        {
            string hashed_password = string.Empty;

            int key = password.Length;

            hashed_password = Hash(password, key);

            return hashed_password;
        }

        private static string Hash(string value, int key)
        {
            string encryption = string.Empty;
            string reverse = string.Empty;

            try
            {
                if (!value.Equals(string.Empty))
                {
                    for (int i = value.Length - 1; i >= 0; i--)
                        reverse += value[i];

                    char[] letters = reverse.ToCharArray();
                    for (int i = 0; i < letters.Length; i++)
                        encryption += (char)((((int)letters[i] * key) % (MAX_VALUE - MIN_VALUE)) + MIN_VALUE);
                }
            }
            catch
            {
                return value;
            }

            return encryption;
        }


        public static string Encrypt(string value)
        {
            string encryption = string.Empty;
            string characters = string.Empty;

            try
            {
                if (!value.Equals(string.Empty))
                {
                    int key = new System.Random().Next((MIN_VALUE - MIN_VALUE) + 1, (MAX_VALUE - MIN_VALUE));

                    for (int i = value.Length - 1; i >= 0; i--)
                    {
                        encryption += value[i];

                        if (!characters.Contains(value[i].ToString()))
                            characters += value[i];
                    }

                    foreach (char c in characters)
                    {
                        encryption = encryption.Replace(c, (char)((int)c * key));
                    }

                    encryption += (char)key;
                }
            }
            catch
            {
                return value;
            }

            return encryption;
        }

        public static string Decrypt(string value)
        {
            string decryption = string.Empty;
            string characters = string.Empty;

            try
            {
                if (!value.Equals(string.Empty))
                {
                    int key = (int)(char.Parse(value.Substring(value.Length - 1)));

                    value = value.Substring(0, (value.Length - 1));

                    for (int i = value.Length - 1; i >= 0; i--)
                    {
                        decryption += value[i];

                        if (!characters.Contains(value[i].ToString()))
                            characters += value[i];
                    }

                    foreach (char c in characters)
                    {
                        decryption = decryption.Replace(c, (char)(c / key));
                    }
                }
            }
            catch
            {
                return value;
            }

            return decryption;
        }
        #endregion
        //*************************************************
    }
}